package com.hm.base.api;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.hm.base.auto.helper.HmSessionFactory;
import com.hm.base.auto.su.ControllerResult;
import com.hm.base.auto.su.R.Restful;
import com.hm.base.service.DataAuthService;
import com.hm.base.vo.DataAuthVo;
import com.hm.common.su.bean.PageInfo;
import com.hm.common.su.bean.PageInfo.PageParam;
import com.hm.common.su.bean.ServerResponse;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;

/**
 * @author shishun.wang
 * @date 2018年1月25日 下午2:19:33
 * @version 1.0
 * @describe 
 */
@Slf4j
@ResponseBody
@RestController
@RequestMapping(Restful.API + "/dtauth")
public class DataAuthApi {

	private static final String SWWAGER_TAG = "数据权限管理";	
	
	@Autowired
	private DataAuthService dataAuthService;
	
	@RequestMapping(method = RequestMethod.POST)
	@ApiOperation(tags = SWWAGER_TAG, value = "新增数据权限信息", httpMethod = "POST", notes = "com.hm.base.api.DataAuthApi::addDataAuth")
	public ResponseEntity<ServerResponse<Boolean>> addDataAuth(
			@ApiParam(required = true, name = "body") @RequestBody(required = true) DataAuthVo dataAuthVo) {
		log.debug("用户{}新增数据权限信息", HmSessionFactory.currentUserId());
		dataAuthVo.setCreateUser(HmSessionFactory.currentUserId());
		dataAuthService.addDataAuth(dataAuthVo);
		return ControllerResult.success(true);
	}

	@RequestMapping(value = "/{dataAuthId}", method = RequestMethod.GET)
	@ApiOperation(tags = SWWAGER_TAG, value = "获取数据权限信息", httpMethod = "GET", notes = "com.hm.base.api.DataAuthApi::getDataAuth")
	public ResponseEntity<ServerResponse<DataAuthVo>> getDataAuth(
			@ApiParam(required = true, name = "dataAuthId", value = "数据权限ID") @PathVariable("dataAuthId") Long dataAuthId) {
		log.debug("用户{}获取数据权限信息", HmSessionFactory.currentUserId());
		return ControllerResult.success(dataAuthService.getDataAuth(dataAuthId));
	}

	@RequestMapping(value = "/{dataAuthId}", method = RequestMethod.PUT)
	@ApiOperation(tags = SWWAGER_TAG, value = "修改数据权限信息", httpMethod = "PUT", notes = "com.hm.base.api.DataAuthApi::updateDataAuth")
	public ResponseEntity<ServerResponse<Boolean>> updateDataAuth(
			@ApiParam(required = true, name = "dataAuthId", value = "数据权限ID") @PathVariable("dataAuthId") Long dataAuthId,
			@ApiParam(required = true, name = "body") @RequestBody(required = true) DataAuthVo dataAuthVo) {
		log.debug("用户{}修改数据权限信息", HmSessionFactory.currentUserId());
		dataAuthVo.setId(dataAuthId);
		dataAuthService.updateDataAuth(dataAuthVo);
		return ControllerResult.success(true);
	}

	@RequestMapping(method = RequestMethod.DELETE)
	@ApiOperation(tags = SWWAGER_TAG, value = "删除数据权限信息", httpMethod = "DELETE", notes = "com.hm.base.api.DataAuthApi::deleteDataAuth")
	public ResponseEntity<ServerResponse<Boolean>> deleteDataAuth(
			@ApiParam(required = true, name = "dataAuthIds", value = "数据权限ID") @RequestParam(required = true, name = "dataAuthIds") List<Long> dataAuthIds) {
		log.debug("用户{}删除数据权限信息", HmSessionFactory.currentUserId());
		dataAuthService.deleteDataAuth(dataAuthIds);
		return ControllerResult.success(true);
	}
	
	@RequestMapping(value = "/{pageNumber}/{pageSize}", method = RequestMethod.GET)
	@ApiOperation(tags = SWWAGER_TAG, value = "分页系统数据权限记录", httpMethod = "GET", notes = "com.hm.base.api.DataAuthApi::query")
	public ResponseEntity<ServerResponse<PageInfo<DataAuthVo>>> query(
			@ApiParam(required = true, name = "pageNumber", value = "分页号(第一页值为1,最小页码不能小于1)") @PathVariable("pageNumber") int pageNumber,
			@ApiParam(required = true, name = "pageSize", value = "分页大小") @PathVariable("pageSize") int pageSize,
			@ApiParam(required = false, name = "module", value = "模块") @RequestParam(required = false, name = "module") String module,
			@ApiParam(required = false, name = "name", value = "名称") @RequestParam(required = false, name = "name") String name,
			@ApiParam(required = false, name = "authCode", value = "授权码") @RequestParam(required = false, name = "authCode")String authCode)
			throws Exception {
		log.debug("用户{},查询分页系统数据权限记录", HmSessionFactory.currentUserId());
		return ControllerResult.success(dataAuthService.query(new PageParam(pageNumber, pageSize), name, module, authCode));
	}
}
